####################################################
#
# REPLICATION "Choices that Matter: Choices that Matter: Coalition Formation and Parties’ Ideological Reputations"
# Authors:		Albert Falco
#				Pablo Fernandez-Vazquez
# Software:		R 3.5.1
# Data sources:	2010_BES_internetpanel.dta
#				2010_BES_facetoface.dta
#				2009_Ger_panel.dta
#				replication_data_simex.dta
#
####################################################

# Packages
	install.packages("ggplot2", repos = "http://cran.us.r-project.org")
	install.packages("foreign", repos = "http://cran.us.r-project.org")
	install.packages("weights", repos = "http://cran.us.r-project.org")
	install.packages("dplyr", repos = "http://cran.us.r-project.org")
	install.packages( "tidyverse", repos = "http://cran.us.r-project.org" )
	install.packages( "haven", repos = "http://cran.us.r-project.org" )
	install.packages ( "DataCombine", repos = "http://cran.us.r-project.org" )
	install.packages( "simex", repos = "http://cran.us.r-project.org" )
	install.packages( "margins", repos = "http://cran.us.r-project.org" )
	install.packages( "ggplot2", repos = "http://cran.us.r-project.org" )

	library(foreign)
	library(ggplot2)
	library(weights)
	library(dplyr)
	library( tidyverse )
	library( haven )
	library ( DataCombine )
	library( simex )
	library( margins )
	library( ggplot2 )


# Creating LOG FILE
	sink( "replication_logFile_R.txt" )

# Loading Data

	# 2010 British Election Study - Internet Survey
		# Data, questionnaires and documentation for the 2010 Internet Panel British Election Study. aka Campaign Panel Internet Survey (CIPS) can be found: http://bes2009-10.org/bes-data.php
			bes10_internet <- read.dta("2010_BES_internetpanel.dta", convert.factors = F )

	# 2010 British Election Study - Face-to-face interviews
		# The data file and part of the documentation for 2010 British Election Study (face-to-face surveys) can be found here: http://www.britishelectionstudy.com/data-object/2010-bes-cross-section/
		# Remaining documentation (questionnaires mainly) are to be found here: http://bes2009-10.org/bes-data.php
		# The data refer to face-to-face interviews. There is a pre- and a post- wave. Also a short mailback questionnaire. For documentation, check 2010BESTechnicalReport.pdf
			bes10_face <- read.dta("2010_BES_facetoface.dta", convert.factors = F )

	# 2009 German Longitudinal Panel Study (GLES)
		# Source: ZACAT study ZA5302. See https://dbk.gesis.org/dbksearch/SDesc2.asp?no=5302&tab=3&ll=10&notabs=&af=&nf=1&search=5302&search2=&db=E
		# Pre-election and post-election waves
		# CAPI interviews
			ger09 <- read.dta("2009_Ger_panel.dta" ,convert.factors = F )

	# Time-Series Cross Section Data for SIMEX Analyses
			aggSE <- read.dta("replication_data_simex.dta" ,convert.factors = F )


# TABLE 4
	# Pre-election placement of political parties
		# Taxes and spending
			# aaq105 - Labour placement, aaq106 - Conservative placement, aaq107 - Liberal Democrat placement
				apply( bes10_internet[ , c( "aaq107", "aaq106", "aaq105" ) ], 2, function(x) mean( x, na.rm = T ) ) %>% round( digits = 1 )

		# Fighting crime tradeoff
			# aq38_2 - Labour placement, aq38_3 - Conservative placement, aq38_4 - Liberal Democrat placement
				apply( bes10_face[ , c( "aq38_4", "aq38_3", "aq38_2" ) ], 2, function(x) mean( x, na.rm = T ) ) %>% round( digits = 1 )


	# Percentage of respondents who think a coalition with Conservative Party would be a more ideologically consistent choice.
		
		# Taxes and spending
			# Perceived distance between the Liberal Democrats and a coalition with the Conservatives
				bes10_internet$ldtocon <- abs( bes10_internet$aaq107 - ( .16*bes10_internet$aaq107 + .84*bes10_internet$aaq106 ))
			# Perceived distance between the Liberal Democrats and a coalition with Labour
				bes10_internet$ldtolab <- abs(bes10_internet$aaq107 - ( .18*bes10_internet$aaq107 + .82*bes10_internet$aaq105 ))
			# Keeping only non-NA observations
				dist_taxes <- na.omit( data.frame( ldtocon = bes10_internet$ldtocon, ldtolab = bes10_internet$ldtolab ))	
			# PERCENTAGE who think the Conservative Coalition is closer
				100 * sum( dist_taxes$ldtocon < dist_taxes$ldtolab)/(sum(dist_taxes$ldtocon > dist_taxes$ldtolab) + sum(dist_taxes$ldtocon < dist_taxes$ldtolab))

		# Fighting crime trade-off
			# Perceived distance between the Liberal Democrats and a coalition with the Conservatives
				bes10_face$crimeldtocon <- abs(bes10_face$aq38_4 - (.16*bes10_face$aq38_4 + .84*bes10_face$aq38_3 ))
			# Perceived distance between the Liberal Democrats and a coalition with Labour
				bes10_face$crimeldtolab <- abs(bes10_face$aq38_4 - (.18*bes10_face$aq38_4 + .82*bes10_face$aq38_2 ))
			# Keeping only non-NA observations
				dist_crime <- na.omit( data.frame( crimeldtocon = bes10_face$crimeldtocon, crimeldtolab = bes10_face$crimeldtolab ))
			# PERCENTAGE who think the Conservative Coalition is closer
				100 * sum( dist_crime$crimeldtocon < dist_crime$crimeldtolab)/(sum(dist_crime$crimeldtocon > dist_crime$crimeldtolab) + sum(dist_crime$crimeldtocon < dist_crime$crimeldtolab))  



# TABLE 5

	# Defining which individuals were interviewed AFTER the COALITION was ANNOUNCED
		bes10_internet$aftercoalition <- as.numeric( bes10_internet$ccps_res >= as.Date("20100511", format="%Y%m%d")) # Cameron announces coalition on May 11
	
	# LIBERAL DEMOCRATS: Perceived position *before* and *after* the formation of the coalition -- relevant variable CCQ113
		aggregate( bes10_internet$ccq113, list( aftercoalition = bes10_internet$aftercoalition ), mean, na.rm=T ) %>% round( digits = 1 )
			# Difference
				mean( bes10_internet$ccq113 [ bes10_internet$aftercoalition == 0 ], na.rm = T ) - mean( bes10_internet$ccq113[ bes10_internet$aftercoalition ==1 ], na.rm = T )
			# t-test p-value
				t.test( bes10_internet$ccq113 [ bes10_internet$aftercoalition == 0 ], bes10_internet$ccq113[ bes10_internet$aftercoalition == 1 ], alternative = "greater" )$p.value
	
	# CONSERVATIVE PARTY: Perceived position *before* and *after* the formation of the coalition -- relevant variable CCQ112
		aggregate( bes10_internet$ccq112, list( aftercoalition = bes10_internet$aftercoalition ), mean, na.rm=T ) %>% round( digits = 1 )
			# Difference
				diffcon <- mean( bes10_internet$ccq112 [ bes10_internet$aftercoalition == 0 ], na.rm = T ) - mean( bes10_internet$ccq112[ bes10_internet$aftercoalition ==1 ], na.rm = T )
				round( diffcon, digits = 2 )
			# t-test p-value
				t.test( bes10_internet$ccq112 [ bes10_internet$aftercoalition == 0 ], bes10_internet$ccq112[ bes10_internet$aftercoalition == 1 ], alternative = "less" )$p.value

	# LABOUR PARTY: Perceived position *before* and *after* the formation of the coalition -- relevant variable CCQ111
		aggregate( bes10_internet$ccq111, list( aftercoalition = bes10_internet$aftercoalition ), mean, na.rm=T ) %>% round( digits = 1 )
			# Difference
				difflab <- mean( bes10_internet$ccq111 [ bes10_internet$aftercoalition == 0 ], na.rm = T ) - mean( bes10_internet$ccq111[ bes10_internet$aftercoalition ==1 ], na.rm = T )
				round( difflab, digits = 2 )
			# t-test p-value
				t.test( bes10_internet$ccq111 [ bes10_internet$aftercoalition == 0 ], bes10_internet$ccq111[ bes10_internet$aftercoalition == 1 ] )$p.value
	
	# N in each group
		sum(!is.na( bes10_internet$ccq113[ bes10_internet$aftercoalition==0 ]))
		sum(!is.na( bes10_internet$ccq113[ bes10_internet$aftercoalition==1 ]))



# TABLE 6
	## ISSUE SCALES:
		# left right variables CDU vn188a, CSU vn188b ... FDP vn188d /// self assessment vn190
		# taxes and public spending: self vn193, CDU vn192a, CSU vn192b, SPD vn192c, FDP vn192d 
		# libertarian dimension: self vn195, CDU vn194a, CSU vn194b, SPD vn194c, FDP vn194d 
		# nuclear power: self vn151, CDU vn152a, CSU vn152b, SPD vn152c, FDP vn152d 

	# recoding left-right
		ger09[, grep("^vn188.*[a-z]$",names(ger09))] <- apply(ger09[, grep("^vn188.*[a-z]$",names(ger09))], 2, function(x) ifelse(x>11, NA, x-1))
		ger09$vn190 <-ifelse(ger09$vn190>11, NA, ger09$vn190-1)
	# recoding taxes and public spending
		ger09[, grep("^vn192.*[a-z]$",names(ger09))] <- apply(ger09[, grep("^vn192.*[a-z]$",names(ger09))], 2, function(x) ifelse(x>11, NA, x-1))
		ger09$vn193 <-ifelse(ger09$vn193>11, NA, ger09$vn193-1)
	# recoding libertarian dimension
		ger09[, grep("^vn194.*[a-z]$",names(ger09))] <- apply(ger09[, grep("^vn194.*[a-z]$",names(ger09))], 2, function(x) ifelse(x>11, NA, x-1))
		ger09$vn195 <-ifelse(ger09$vn195>11, NA, ger09$vn195-1)
	# recoding nuclear issue
		ger09[, grep("^vn152.*[a-z]$",names(ger09))] <- apply(ger09[, grep("^vn152.*[a-z]$",names(ger09))], 2, function(x) ifelse(x>11, NA, x-1))
		ger09$vn151 <-ifelse(ger09$vn151>11, NA, ger09$vn151-1)

	# First, create date variable
		ger09$dat <- as.character( ger09$dat )
		ger09$dat <- as.Date( ger09$dat, format="%Y%m%d" )

	# separating data into waves
		pre <- subset( ger09, ger09$dat <= as.Date("20090926", format="%Y%m%d" ))
		post <- subset( ger09, ger09$dat > as.Date("20090926", format="%Y%m%d" ))

	# LEFT-RIGHT
		# Perceived placements for FDP, CDU, and SPD
			apply( subset( pre, select = c( vn188d, vn188a, vn188c ) ), 2, function(x) mean(x, na.rm=T)) %>% round( digits = 1 )
		# Perceived distance between the FDP and a coalition with the CDU/CSU
			pre$distfdpcducsu <- abs(pre$vn188d - (.28*pre$vn188d + .58*pre$vn188a+ .14*pre$vn188b))
		# Perceived distance between the FDP and a coalition with the SPD and Linke
			pre$distspdlinke <- abs(pre$vn188d - (.30*pre$vn188d + .46*pre$vn188c+ .24*pre$vn188f))
		# Keeping only non-NA observations
			distances_lr <- na.omit( data.frame(distfdpcducsu=pre$distfdpcducsu,distspdlinke=pre$distspdlinke))
		# Percentage of respondents who thinks FDP is closer to COALITION with CDU/CSU
			100 * sum(as.numeric(distances_lr$distfdpcducsu < distances_lr$distspdlinke)) / (sum(as.numeric(distances_lr$distfdpcducsu < distances_lr$distspdlinke)) + sum(as.numeric(distances_lr$distfdpcducsu > distances_lr$distspdlinke)))
	
	# TAXES and SPENDING
		# Perceived placements for FDP, CDU, and SPD
			apply( subset( pre, select = c( vn192d, vn192a, vn192c ) ), 2, function(x) mean(x, na.rm=T)) %>% round( digits = 1 )
		# Perceived distance between the FDP and a coalition with the CDU/CSU
			pre$dtaxcdu <- abs(pre$vn192d - (.28*pre$vn192d + .58*pre$vn192a+ .14*pre$vn192b))
		# Perceived distance between the FDP and a coalition with the SPD and Linke
			pre$dtaxspdlinke <- abs(pre$vn192d - (.30*pre$vn192d + .46*pre$vn192c+ .24*pre$vn192f))
		# Keeping only non-NA observations
			distances_tax <- na.omit(data.frame(dtaxcdu=pre$dtaxcdu,dtaxspdlinke=pre$dtaxspdlinke)) 
		# Percentage of respondents who thinks FDP is closer to COALITION with CDU/CSU
			100 * sum(as.numeric(distances_tax$dtaxcdu < distances_tax$dtaxspdlinke)) / (sum(as.numeric(distances_tax$dtaxcdu < distances_tax$dtaxspdlinke)) + sum(as.numeric(distances_tax$dtaxcdu > distances_tax$dtaxspdlinke)))

	# LIBERTARIAN DIMENSION
		# Perceived placements for FDP, CDU, and SPD
			apply( subset( pre, select = c( vn194d, vn194a, vn194c ) ), 2, function(x) mean(x, na.rm=T)) %>% round( digits = 1 )
		# Perceived distance between the FDP and a coalition with the CDU/CSU
			pre$dimmcdu <- abs(pre$vn194d - (.28*pre$vn194d + .58*pre$vn194a+ .14*pre$vn194b))
		# Perceived distance between the FDP and a coalition with the SPD and Linke
			pre$dimmspdlinke <- abs(pre$vn194d - (.30*pre$vn194d + .46*pre$vn194c+ .24*pre$vn194f))
		# Keeping only non-NA observations
			distances_libert <- na.omit(data.frame(dimmcdu=pre$dimmcdu,dimmspdlinke=pre$dimmspdlinke)) 
		# Percentage of respondents who thinks FDP is closer to COALITION with CDU/CSU
			100 * sum(as.numeric(distances_libert$dimmcdu < distances_libert$dimmspdlinke)) / (sum(as.numeric(distances_libert$dimmcdu < distances_libert$dimmspdlinke)) + sum(as.numeric(distances_libert$dimmcdu > distances_libert$dimmspdlinke)))

	# NUCLEAR POLICY
		# Perceived placements for FDP, CDU, and SPD
			apply( subset( pre, select = c( vn152d, vn152a, vn152c ) ), 2, function(x) mean(x, na.rm=T)) %>% round( digits = 1 )
		# Perceived distance between the FDP and a coalition with the CDU/CSU
			pre$dnuccdu <- abs(pre$vn152d - (.28*pre$vn152d + .58*pre$vn152a+ .14*pre$vn152b))
		# Perceived distance between the FDP and a coalition with the SPD and Linke
			pre$dnucspdlinke <- abs(pre$vn152d - (.30*pre$vn152d + .46*pre$vn152c+ .24*pre$vn152f))
		# Keeping only non-NA observations
			distances_nuke <- na.omit(data.frame(dnuccdu=pre$dnuccdu,dnucspdlinke=pre$dnucspdlinke)) 
		# Percentage of respondents who thinks FDP is closer to COALITION with CDU/CSU
			100 * sum(as.numeric(distances_nuke$dnuccdu < distances_nuke$dnucspdlinke)) / (sum(as.numeric(distances_nuke$dnuccdu < distances_nuke$dnucspdlinke)) + sum(as.numeric(distances_nuke$dnuccdu > distances_nuke$dnucspdlinke)))



# TABLE 7
	# Perceived FDP placement before and after the coalition is announced
	# Indicator that interview happened after coalition is announced (only post-election wave)
		post$aftercoal <- ifelse(post$dat>as.Date("20091024", format="%Y%m%d"),1,0)

		# LEFT-RIGHT dimension
			aggregate(post$vn188d, list(aftercoalition=post$aftercoal), mean, na.rm=T) %>% round( digits = 1)
				# Difference
					mean( post$vn188d[ post$aftercoal == 0 ], na.rm = T ) - mean( post$vn188d[ post$aftercoal == 1 ], na.rm = T )
				# p-value
					t.test(post$vn188d[post$aftercoal==0], post$vn188d[post$aftercoal==1], alternative="less")$p.value

		# TAXES and SPENDING dimension
			aggregate(post$vn192d, list(aftercoalition=post$aftercoal), mean, na.rm=T) %>% round( digits = 1 )
				# Difference
					mean( post$vn192d[ post$aftercoal == 0 ], na.rm = T ) - mean( post$vn192d[ post$aftercoal == 1 ], na.rm = T )
				# p-value
					t.test(post$vn192d[post$aftercoal==0], post$vn192d[post$aftercoal==1], alternative="less")$p.value

		# LIBERTARIAN dimension
			aggregate(post$vn194d, list(aftercoalition=post$aftercoal), mean, na.rm=T) %>% round( digits = 1 )
				# Difference
					mean( post$vn194d[ post$aftercoal == 0 ], na.rm = T ) - mean( post$vn194d[ post$aftercoal == 1 ], na.rm = T )
				# p-value
					t.test(post$vn192d[post$aftercoal==0], post$vn192d[post$aftercoal==1], alternative="less")$p.value

		# NUCLEAR dimension
			aggregate(post$vn152d, list(aftercoalition=post$aftercoal), mean, na.rm=T)  %>% round( digits = 1 )
				# Difference
					diffNuk <- mean( post$vn152d[ post$aftercoal == 0 ], na.rm = T ) - mean( post$vn152d[ post$aftercoal == 1 ], na.rm = T )
					round( diffNuk, digits = 2 )
				# p-value
					t.test(post$vn152d[post$aftercoal==0], post$vn152d[post$aftercoal==1], alternative="greater")$p.value

		# N - sample sizes
			cbind( nrow( subset( post, aftercoal == 0 ) ), nrow( subset( post, aftercoal == 1 ) ) )



############### ONLINE APPENDIX

# TABLE A4

	# Removing incomplete observations
		aggSE_complete <- aggSE %>% mutate (  country_id = as.factor ( country_id ) ) %>% select( update, diffgovw, party_rg_part, caltw_ap90_, country_id, sample, update_se, diffgovw_SE, distance, distanceSE, alternative, party_status ) %>% filter( sample == 1 ) %>% na.omit()
	# Setting the seed
		set.seed(1981)
	
	# SIMEX model
		simex_model <- simex( model = lm( update ~ distance * party_status * alternative + country_id,
			data = aggSE_complete, x = TRUE ),
		SIMEXvariable=c( "update", "distance" ),
		measurement.error = cbind( aggSE_complete$update_se, aggSE_complete$distanceSE ),
		lambda = c(0.5, 1, 1.5, 2, 2.5, 3),
		B = 100, fitting.method = "quadratic", jackknife.estimation = "quadratic", asymptotic = F )

		summary( simex_model )



# FIGURE A4

	# Predicted change
		dataforprediction_simex_fig1_main <- data.frame( distance = rep ( seq( -7, 7, 1 ), 2 ), party_status = rep( 1, 30 ),  alternative = c( rep( 0, 15 ), rep( 1, 15 ) ), country_id = rep( "8", 30 ) )
		simex_model_figure1_main_predicted <- prediction( simex_model, data = dataforprediction_simex_fig1_main ) %>% as.data.frame()
		simex_model_figure1_main_predicted <- simex_model_figure1_main_predicted %>% mutate ( CLOSER_ALTERNATIVE = factor( alternative, levels = c( 1,0 ), labels = c( "YES", "NO" ) ) ) 
		simex_model_figure1_main_predicted <- simex_model_figure1_main_predicted %>% mutate ( lb = fitted - 1.96*se.fitted, ub = fitted + 1.96*se.fitted ) 

	# PLOT: There is No Ideologically Closer Alternative Coalition Alternative = 0
		simex_main_alternative0_PLOT <- ggplot( data = subset( simex_model_figure1_main_predicted, CLOSER_ALTERNATIVE == "NO" ) ) + scale_x_continuous( limits = c( -7, 7 ) , name="Distance to Real Government Position", labels = seq( -7, 7, 1 ), breaks = seq( -7, 7, 1 ) ) + scale_y_continuous( limits = c( -4, 4 ) ) + ylab("Predicted Update in Left-Right Perception") + xlab(NULL) + ggtitle("There is No Ideologically Closer Alternative Coalition\nAlternative = 0")
		simex_main_alternative0_PLOT <- simex_main_alternative0_PLOT + geom_linerange( aes( ymin = lb, ymax = ub, x = distance ), color = I("#61BEFF"), size = 1 )
		simex_main_alternative0_PLOT <- simex_main_alternative0_PLOT + geom_point( aes( y = fitted, x = distance ), color = I("#61BEFF"), size=2)
		simex_main_alternative0_PLOT <- simex_main_alternative0_PLOT + geom_hline( yintercept = 0,  color = "red", linetype = "dashed" )
		simex_main_alternative0_PLOT <- simex_main_alternative0_PLOT + theme(panel.background = element_rect(fill = "white", colour = "grey0"))
		simex_main_alternative0_PLOT
	
	# PLOT: There is Ideologically Closer Alternative Coalition Alternative = 1
		simex_main_alternative1_PLOT <- ggplot( data = subset( simex_model_figure1_main_predicted, CLOSER_ALTERNATIVE == "YES" ) ) + scale_x_continuous( limits = c( -7, 7 ) , name="Distance to Real Government Position", labels = seq( -7, 7, 1 ), breaks = seq( -7, 7, 1 ) ) + scale_y_continuous( limits = c( -4, 4 ) ) + ylab("Predicted Update in Left-Right Perception") + xlab(NULL) + ggtitle("There is Ideologically Closer Alternative Coalition\nAlternative = 1")
		simex_main_alternative1_PLOT <- simex_main_alternative1_PLOT + geom_linerange( aes( ymin = lb, ymax = ub, x = distance ), color = I("#F8766D"), size = 1 )
		simex_main_alternative1_PLOT <- simex_main_alternative1_PLOT + geom_point( aes( y = fitted, x = distance ), color = I("#F8766D"), size=2)
		simex_main_alternative1_PLOT <- simex_main_alternative1_PLOT + geom_hline( yintercept = 0,  color = "red", linetype = "dashed" )
		simex_main_alternative1_PLOT <- simex_main_alternative1_PLOT + theme(panel.background = element_rect(fill = "white", colour = "grey0"))
		simex_main_alternative1_PLOT


# FIGURE B2

	# Monte Carlo Simulation
	# Most of Code for this figure is shown in FIgure B2.

		# first I create empty vectors
			avperceptionclosertoright <- rep(NA, 50000)
			avperceptionclosertoleft <- rep(NA, 50000)
			
			avperceptionclosertorightPOST <- rep(NA, 50000)
			avperceptionclosertoleftPOST <- rep(NA, 50000)
			
			avshiftclosertoright <- rep(NA, 50000)
			avshiftclosertoleft <- rep(NA, 50000)
			

		# Container of seeds
			seeds <- seq(1:50000)

			for(i in 1:50000){
				set.seed(seeds[i])
				left1 <- rnorm(100, 2, 1) 
				left2 <- rnorm(100, 3, 1)
				right1 <- rnorm(100, 8, 1) 
				right2 <- rnorm(100, 7, 1)
				centre <- rnorm(100, 5, 1)
				
				rightCab <- (.2*right1 + .2*right2 + .1*centre) / .5 
				leftCab <- (.2*left1 + .2*left2 + .1*centre) / .5
				
				alternative <- abs(rightCab - centre) < abs(leftCab - centre)
				
				avperceptionclosertoright[i] <- mean ( centre [ alternative == 1 ] ) 
				avperceptionclosertoleft[i] <- mean ( centre [ alternative == 0 ] )
				
				newcentre <- rnorm(100, 5, 1)
				
				avperceptionclosertorightPOST[i] <- mean( newcentre [ alternative == 1 ] ) 
				avperceptionclosertoleftPOST[i] <- mean ( newcentre [ alternative == 0 ] )
				
				outcome <- newcentre - centre 
				
				avshiftclosertoright[i] <- mean(outcome [ alternative == 1 ])
				avshiftclosertoleft[i] <- mean(outcome [ alternative == 0 ])		
			}


		# Graphing results
			perception <- c ( avperceptionclosertoright , avperceptionclosertoleft , avperceptionclosertorightPOST , avperceptionclosertoleftPOST)
			timing <- factor ( c ( rep ( "before", length ( c ( avperceptionclosertoright , avperceptionclosertoleft) ) ) ,  rep ( "after", length ( c ( avperceptionclosertorightPOST , avperceptionclosertoleftPOST ) ) ) ), levels = c ( "before", "after" ) )
			group <- factor ( c ( rep ( "closertoright" , length (avperceptionclosertoright ) ) , rep ( "closertoleft" , length (avperceptionclosertoleft ) ) , rep ( "closertoright" , length ( avperceptionclosertorightPOST ) ) , rep ( "closertoleft" , length (avperceptionclosertoleftPOST ) ) ) , levels = c ( "closertoright", "closertoleft" ) , labels = c ( "RIGHT coalition is closer" , "LEFT coalition is closer" ))

			graph1data <- data.frame(perception, timing, group)


			figure_b1 <- ggplot(data=graph1data)
			figure_b1 <- figure_b1 + geom_density( aes ( x = perception, color = timing ) , na.rm=T, size=1.05 ) + facet_grid( group ~ .)
			figure_b1



# TABLE C1
	# Balance tests. 2010 British Campaign Study.

	# MARITAL STATUS -- Survey question: aaq161
			table( bes10_internet$aaq161 )
			bes10_internet$aa_married <- ifelse( bes10_internet$aaq161 == 1, 1, 0) # Creating indicator of whether married
		# averages
			aggregate( bes10_internet$aa_married, list( aftercoalition = bes10_internet$aftercoalition ), mean, na.rm=T ) %>% round( digits = 2 )
		# difference
			diffmar <- mean( bes10_internet$aa_married[ bes10_internet$aftercoalition == 1 ], na.rm = T ) - mean( bes10_internet$aa_married[ bes10_internet$aftercoalition == 0 ], na.rm = T )
			round( diffmar, digits = 2)
		# p-value
			t.test(bes10_internet$aa_married[bes10_internet$aftercoalition==0], bes10_internet$aa_married[bes10_internet$aftercoalition==1])$p.value

	# GENDER -- Survey question: aaq186
			table( bes10_internet$aaq186 )
			bes10_internet$aaq186 <- bes10_internet$aaq186 -1 # recoding so that variable takes value of 1 if woman
		# averages
			aggregate( bes10_internet$aaq186, list( aftercoalition = bes10_internet$aftercoalition ), mean, na.rm=T ) %>% round( digits = 2 )
		# difference
			mean( bes10_internet$aaq186[ bes10_internet$aftercoalition == 1 ], na.rm = T ) - mean( bes10_internet$aaq186[ bes10_internet$aftercoalition == 0 ], na.rm = T )
		# p-value
			t.test(bes10_internet$aaq186[bes10_internet$aftercoalition==0], bes10_internet$aaq186[bes10_internet$aftercoalition==1])$p.value

	# EDUCATION -- Survey question: aaq157
			table( bes10_internet$aaq157 )
			bes10_internet$educbelow15 <- ifelse(bes10_internet$aaq157==1, 1, 0) # % finished education at 15 or younger
		# averages
			aggregate( bes10_internet$educbelow15, list( aftercoalition = bes10_internet$aftercoalition ), mean, na.rm=T ) %>% round( digits = 2 )
		# difference
			mean( bes10_internet$educbelow15[ bes10_internet$aftercoalition == 0 ], na.rm = T ) - mean( bes10_internet$educbelow15[ bes10_internet$aftercoalition == 1 ], na.rm = T )
		# p-value
			t.test(bes10_internet$educbelow15[bes10_internet$aftercoalition==0], bes10_internet$educbelow15[bes10_internet$aftercoalition==1])$p.value

	# INCOME -- Survey question: aaq166
			table( bes10_internet$aaq166 )
			bes10_internet$aaq166 <- ifelse(bes10_internet$aaq166>=17, NA, bes10_internet$aaq166)
		# averages
			aggregate( bes10_internet$aaq166, list( aftercoalition = bes10_internet$aftercoalition ), mean, na.rm=T ) %>% round( digits = 1 )
		# difference
			diffinc <- mean( bes10_internet$aaq166[ bes10_internet$aftercoalition == 1 ], na.rm = T ) - mean( bes10_internet$aaq166[ bes10_internet$aftercoalition == 0 ], na.rm = T )
			round( diffinc, digits = 1)
		# p-value
			t.test(bes10_internet$aaq166[bes10_internet$aftercoalition==0], bes10_internet$aaq166[bes10_internet$aftercoalition==1])$p.value

	# RELIGIOUS -- Survey question: aaq163
			table(bes10_internet$aaq163)
			bes10_internet$aa_religious <- ifelse(bes10_internet$aaq163==1, 1, 0)
		# averages
			aggregate( bes10_internet$aa_religious, list( aftercoalition = bes10_internet$aftercoalition ), mean, na.rm=T ) %>% round( digits = 2 )
		# difference
			diffrel <- mean( bes10_internet$aa_religious[ bes10_internet$aftercoalition == 0 ], na.rm = T ) - mean( bes10_internet$aa_religious[ bes10_internet$aftercoalition == 1 ], na.rm = T )
			round( diffrel, digits = 2)
		# p-value
			t.test(bes10_internet$aa_religious[bes10_internet$aftercoalition==0], bes10_internet$aa_religious[bes10_internet$aftercoalition==1])$p.value

	# HOUSING -- Survey question: aaq153
			table(bes10_internet$aaq153)
			bes10_internet$aa_own <- ifelse(bes10_internet$aaq153==1, 1, 0) # whether respondent owns a house
		# averages
			aggregate( bes10_internet$aa_own, list( aftercoalition = bes10_internet$aftercoalition ), mean, na.rm=T ) %>% round( digits = 2 )
		# difference
			diffrel <- mean( bes10_internet$aa_own[ bes10_internet$aftercoalition == 0 ], na.rm = T ) - mean( bes10_internet$aa_own[ bes10_internet$aftercoalition == 1 ], na.rm = T )
			round( diffrel, digits = 2)
		# p-value
			t.test(bes10_internet$aa_own[bes10_internet$aftercoalition==0], bes10_internet$aa_own[bes10_internet$aftercoalition==1])$p.value

	# ETHNICITY -- Survey question: aaq177
			table(bes10_internet$aaq177)
			bes10_internet$aa_nonwhite <- ifelse(bes10_internet$aaq177==1, 0, 1) # whether respondent is NOT white
		# averages
			aggregate( bes10_internet$aa_nonwhite, list( aftercoalition = bes10_internet$aftercoalition ), mean, na.rm=T ) %>% round( digits = 2 )
		# difference
			diffwhite <- mean( bes10_internet$aa_nonwhite[ bes10_internet$aftercoalition == 0 ], na.rm = T ) - mean( bes10_internet$aa_nonwhite[ bes10_internet$aftercoalition == 1 ], na.rm = T )
			round( diffwhite, digits = 2)
		# p-value
			t.test(bes10_internet$aa_nonwhite[bes10_internet$aftercoalition==0], bes10_internet$aa_nonwhite[bes10_internet$aftercoalition==1])$p.value

	# AGE -- Survey question: q151
			table(bes10_internet$aaq151)
			bes10_internet$aa_age <- 2010 - (1899 + bes10_internet$aaq151)
		# averages
			aggregate( bes10_internet$aa_age, list( aftercoalition = bes10_internet$aftercoalition ), mean, na.rm=T ) %>% round( digits = 0 )
		# difference
			diffage <- mean( bes10_internet$aa_age[ bes10_internet$aftercoalition == 0 ], na.rm = T ) - mean( bes10_internet$aa_age[ bes10_internet$aftercoalition == 1 ], na.rm = T )
			round( diffage, digits = 0)
		# p-value
			t.test(bes10_internet$aa_age[bes10_internet$aftercoalition==0], bes10_internet$aa_age[bes10_internet$aftercoalition==1])$p.value

	# TRADE UNION -- Survey question: aaq162
			table(bes10_internet$aaq162)
			bes10_internet$aa_union <- ifelse(bes10_internet$aaq162==1, 1, 0)
		# averages
			aggregate( bes10_internet$aa_union, list( aftercoalition = bes10_internet$aftercoalition ), mean, na.rm=T ) %>% round( digits = 2 )
		# difference
			diffunion <- mean( bes10_internet$aa_union[ bes10_internet$aftercoalition == 1 ], na.rm = T ) - mean( bes10_internet$aa_union[ bes10_internet$aftercoalition == 0 ], na.rm = T )
			round( diffunion, digits = 2)
		# p-value
			t.test(bes10_internet$aa_union[bes10_internet$aftercoalition==0], bes10_internet$aa_union[bes10_internet$aftercoalition==1])$p.value

	# SAMPLE SIZE
			cbind( nrow( subset( bes10_internet, aftercoalition == 0 ) ), nrow( subset( bes10_internet, aftercoalition == 1 ) ) )



# TABLE C2
	# Balance Tests. 2009 German Longitudinal Election Study.

	# AGE -- Survey question: vn542
		# averages
			aggregate(post$vn542, list(timing=post$aftercoal), mean, na.rm=T) %>% round( digits = 0 )
		# p-value
			t.test(post$vn542[post$aftercoal==0], post$vn542[post$aftercoal==1])$p.value

	# GENDER -- Survey question: vn1
			post$vn1 <- post$vn1 - 1
		# averages
			aggregate(post$vn1, list(timing=post$aftercoal), mean, na.rm=T) %>% round( digits = 2 )
		# p-value
			t.test(post$vn1[post$aftercoal==0], post$vn1[post$aftercoal==1])$p.value

	# MARITAL STATUS
			post$married <- ifelse(post$vn5==1, 1, 0)
		# averages
			aggregate(post$married, list(timing=post$aftercoal), mean, na.rm=T) %>% round( digits = 2 )
		# p-value
			t.test(post$married[post$aftercoal==0], post$married[post$aftercoal==1])$p.value

	# HOUSEHOLD SIZE
			post$vn437 <- ifelse(post$vn437>8, NA, post$vn437)
		# averages
			aggregate(post$vn437, list(timing=post$aftercoal), mean, na.rm=T) %>% round( digits = 1 )
		# p-value
			t.test(post$vn437[post$aftercoal==0], post$vn437[post$aftercoal==1])$p.value

	# INCOME LEVEL
			post$vn75 <- ifelse(post$vn75>11, NA, post$vn75)
		# averages
			aggregate(post$vn75, list(timing=post$aftercoal), mean, na.rm=T) %>% round( digits = 1 )
		# p-value
			t.test(post$vn75[post$aftercoal==0], post$vn75[post$aftercoal==1])$p.value

	# EDUCATION
			post$intermediary <- ifelse(post$vn9==3, 1, 0)
		# averages
			aggregate(post$intermediary, list(timing=post$aftercoal), mean, na.rm=T) %>% round( digits = 2 )
		# p-value
			t.test(post$intermediary[post$aftercoal==0], post$intermediary[post$aftercoal==1])$p.value

	# EMPLOYMENT
			post$fulltime <- ifelse(post$vn17==1,1,0)
		# averages
			aggregate(post$fulltime, list(timing=post$aftercoal), mean, na.rm=T) %>% round( digits = 2 )
		# p-value
			t.test(post$fulltime[post$aftercoal==0], post$fulltime[post$aftercoal==1])$p.value

	# RELIGIOUS DENOMINATION
			post$germanprotestant <- ifelse(post$vn79==1,1,0)
		# averages
			aggregate(post$germanprotestant, list(timing=post$aftercoal), mean, na.rm=T) %>% round( digits = 2 )
		# p-value
			t.test(post$germanprotestant[post$aftercoal==0], post$germanprotestant[post$aftercoal==1])$p.value

	# COUNTRY of BIRTH
			post$bornabroad <- ifelse(post$vn82>3, 1, 0)
		# averages
			aggregate(post$bornabroad, list(timing=post$aftercoal), mean, na.rm=T) %>% round( digits = 2 )
		# p-value
			t.test(post$bornabroad[post$aftercoal==0], post$bornabroad[post$aftercoal==1])$p.value

	# EAST vs WEST GERMANY
			post$ostwest <- post$ostwest - 1
			wtd.t.test(post$ostwest[post$aftercoal==0], post$ostwest[post$aftercoal==1], weight=post$wei_ow[post$aftercoal==0], weighty=post$wei_ow[post$aftercoal==1], samedata=F) # No change

	# SAMPLE SIZE
			cbind( nrow( subset( post, aftercoal == 0 ) ), nrow( subset( post, aftercoal == 1 ) ) )


# Close LOG FILE
	sink()